package animation.sort.algorithms;

import animation.sort.SortAnimation;
import animation.core.AlgorithmMonitorModel;


/**
 * Created by IntelliJ IDEA.
 * User: emilio
 * Date: Aug 12, 2004
 * Time: 10:35:43 AM
 * To change this template use File | Settings | File Templates.
 */
public class HSort
        extends SortAnimation
{

    public String toString()
    {
        return "H Sort";
    }

    protected void sort(int array[])
            throws Exception
    {
        hSort(array, 11);
    }

    /**
     * H-Sort basically in insertion Sort in sets of elements separated by H
     *
     * @param array
     * @param h
     */
    protected void hSort(int[] array, int h)
            throws Exception
    {
        monitorModel.registerInvariant("N", array.length);
        monitorModel.registerInvariant("h", h);
        for (int i = h; i < array.length; i++) {
            monitorModel.setVariableValue("i", i);
            markBox(0, i);
            for (int j = i; j >= h && greater(array, j - h, j); j -= h) {
                monitorModel.setVariableValue("j", j);
                monitorModel.setVariableValue("j-h", j-h);
                swap(array, j, j - h);
            }
        }
    }

    protected void registerSpecificVariables(AlgorithmMonitorModel monitorModel) {
        monitorModel.registerVariable("i", "0");
        monitorModel.registerVariable("j", "0");
        monitorModel.registerVariable("j-h", "0");
    }

}
